/*
 * Class ItemOption1
 *
 * Version 1.0
 *
 * Thursday, August 21, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import palidino76.rs2.*;
import palidino76.rs2.player.misc.*;
import palidino76.rs2.player.Player;
import palidino76.rs2.player.items.PlayerItems;
import palidino76.rs2.util.Misc;
import palidino76.rs2.world.items.Items;

public class ItemOption1 implements Packet {
    /**
     * Handles first item options, excluding things such as eating and drinking.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        int itemSlot = p.stream.readUnsignedWordBigEndianA();
        int interfaceId = p.stream.readUnsignedWord();
        int junk = p.stream.readUnsignedWord();
        int itemId = p.stream.readUnsignedWord();
		String itemName = Engine.items.getItemName(itemId);
		PlayerItems pi = new PlayerItems();
		//Items i = new Items();
        if (itemSlot < 0 || itemId < 0) {
            return;
        }
        switch (interfaceId) {
		case 278:
		if(itemId == p.shop2[itemSlot]) {
			p.frames.sendMessage(p, "Some " +Engine.items.getItemName(itemId)+ "s will cost you " +p.shop2p[itemSlot]+ " coins");
		} else if(itemId == p.shop3[itemSlot]) {
			p.frames.sendMessage(p, "This item costs: "+p.shop3p[itemSlot]+ " coins");
		} else if(itemId == p.shop4[itemSlot]) {
			p.frames.sendMessage(p, "This item costs: "+p.shop4p[itemSlot]+ " coins");
		} else if(itemId == p.shop1[itemSlot]) {
			p.frames.sendMessage(p, "This item costs: "+p.shop1p[itemSlot]+ " coins");
		} else if(itemId == p.shop5[itemSlot]) {
			p.frames.sendMessage(p, "This item costs: "+p.shop5p[itemSlot]+ " coins");
		}
		break;


        case 387: // Unequip item.
if(itemId == 4042 || itemId == 4041 || itemId == 4037 || itemId == 4039)
{
p.frames.sendMessage(p, "You cannot take this off.");
}
else
{
            if (itemSlot < p.equipment.length && p.equipment[itemSlot] == itemId) {
                if (!Engine.playerItems.addItem(p, p.equipment[itemSlot], p.equipmentN[itemSlot])) {
                    break;
                }
		if (itemId == 4031) {
			p.convertPlayerToNpc(-1);
			p.updatePlayerAppearance(p.playerWeapon.getWalkEmote(p.equipment[3]), p.playerWeapon.getStandEmote(p.equipment[3]),
				p.playerWeapon.getRunEmote(p.equipment[3]));
			p.updatePlayer(true);
		}
		if (itemId == 4675 || itemId == 9084) {
			p.frames.setTab(p, 79, 192);
		}
		if (itemId == 4037 || itemId == 4039) {
			Server.engine.items.createGroundItem(itemId, p.itemsN[itemSlot], p.absX, p.absY, p.heightLevel, "");
			Server.engine.playerItems.deleteItem(p, itemId, Server.engine.playerItems.getItemSlot(p, itemId), 1);
		}
		if (itemId == 3053) {
			p.frames.sendMessage(p, "You cannot un-equip your battlestaff during the mage arena minigame.");
			return;
		}
		if (p.usingAutoCast) {
			p.castAuto = false;
			p.usingAutoCast = false;
			p.autoCastDelay = 0;
		}
                p.equipment[itemSlot] = -1;
                p.equipmentN[itemSlot] = 0;
	    	PlayerMethods pm = new PlayerMethods(p);
	    	pm.setAttackPlayer(false);
	    	pm.setCombatDelay(0);
                p.frames.setItems(p, 387, 28, 94, p.equipment, p.equipmentN);
                p.playerWeapon.setWeapon();
                p.appearanceUpdateReq = true;
                p.updateReq = true;
                p.calculateEquipmentBonus();
            }
}
            break;
        default:
            Misc.println("[" + p.username + "] Item option 1: " + interfaceId);
            break;
        }
    }
}
